package com.niit.dao.impl;

import java.util.List;

import com.niit.bean.Blog;
import com.niit.dao.BlogDao;
import com.niit.utils.DbUtil;

public class BlogDaoImpl implements BlogDao {

	private DbUtil util = new DbUtil();
	
	@Override
	public int add(Blog t) {
		String sql = "insert into tb_blog (title,author,content) values (?,?,?)";
		int a = util.update(sql, t.getTitle(),t.getAuthor(),t.getContent());
		return a;
	}

	@Override
	public int delete(Blog t) {
		String sql = "delete from tb_blog where blogid = ?";
		int a = util.update(sql, t.getBlogId());
		return a;
	}

	@Override
	public int update(Blog t) {
		String sql = "update tb_blog set title=?,content=?,click=?,checked=? where blogid=?";
		int a = util.update(sql, t.getTitle(),t.getContent(),t.getClick(),t.isChecked(),t.getBlogId());
		return a;
	}

	@Override
	public List<Blog> findAll(int start, int end) {
		String sql = "select * from (select rownum r,tb_blog.* from tb_blog) a where a.r>? and a.r<=?";
		List<Blog> list = util.select(sql, Blog.class, start,end);
		return list;
	}

	@Override
	public int findTotal() {
		String sql = "select count(*) from tb_blog";
		int count = util.selectCount(sql);
		return count;
	}
	
	@Override
	public Blog findByBlogId(String blogId) {
		String sql = "select * from tb_blog where blogId= ?";
		List<Blog> list = util.select(sql, Blog.class, blogId);
		int count = list.size();
		if(count>0){
			return list.get(0);
		}else{
			return null;
		}
	}

}
